import 'dart:convert';

import 'package:cbec_app/components/base/base_ctr.dart';
import 'package:cbec_app/components/http/network.dart';
import 'package:cbec_app/pages/goods/entity/invited_friends_entity.dart';
import 'package:cbec_app/utils/Tools.dart';
import 'package:fluwx/fluwx.dart';
import 'package:get/get_core/get_core.dart';
import 'package:get/get_navigation/get_navigation.dart';
import 'package:get/get_utils/src/platform/platform.dart';
import '../../../utils/AppData.dart';

class InvitedFriendsCtr extends BaseController {
  InvitedFriendsEntity? model;

  @override
  void onInit() {
    super.onInit();
    groupJoinDetail();
  }

  void groupJoinDetail() async {
    Tools.showLoading();
    var bookId = Get.arguments;
    await Network.getGroupJoinDetail(bookId).then((value) {
      model = InvitedFriendsEntity.fromJson(value);
      update();
      Tools.hideLoading();
    }).catchError((error) {
      Tools.hideLoading();
    });
  }

  // 分享小程序卡片
  void shareMiniProgram() async {
    Fluwx fluwx = Fluwx();
    if (GetPlatform.isIOS || GetPlatform.isAndroid) {
      bool isInstall = await fluwx.isWeChatInstalled;
      if (!isInstall) {
        Tools.showToast('请先安装微信');
        return;
      }

      final data = {
        'sid': AppData.sid, // 确保这个值是有效的
        'bookId': model?.bookId,
      };

      // JSON 序列化和 URL 编码
      String jsonData = jsonEncode(data);

      var weChatModel = WeChatShareMiniProgramModel(
          userName: "gh_ac4763917949", //小程序的原始id,如 gh_d43f693ca31f  gh_09674df0c820  gh_ac4763917949
          webPageUrl: 'https://mv2.bd-ego.com/', //兼容老版本小程序的网页连接
          miniProgramType: WXMiniProgramType.test, //小程序类型，测试版本小程序
          path: "pages/guide/index?type=9&data=$jsonData", //小程序的页面路径支持带?xx=121参数
          title: "还差一人，快来拼团", //分享的标题
          description: "麻烦一起来拼个团", //分享的说明文本
          thumbnail: WeChatImage.asset("assets/images/login_logo.png") //分享的图片
          );

      fluwx.share(weChatModel);
    }
  }
}
